/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs     (C)ChaN, 2019        */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be        */
/* attached to the FatFs via a glue function rather than modifying it.   */
/* This is an example of glue functions to attach various exsisting      */
/* storage control modules to the FatFs module with a defined API.       */
/*-----------------------------------------------------------------------*/

#include "ff.h"			/* Obtains integer types */
#include "diskio.h"		/* Declarations of disk functions */
#include "bsp_init.h"   /* 硬件头文件 */

/* Definitions of physical drive number for each drive */
#define FF_SD_CARD		    0	/* Example: Map SD card to physical drive 0 */
#define FF_SPI_FLASH        1	/* Example: Map SPI_FLASH to physical drive 1 */
#define FF_NAND_FLASH		2	/* Example: Map NAND_FLASH to physical drive 2 */


/*-----------------------------------------------------------------------*/
/* Get Drive Status                                                      */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
	BYTE pdrv		/* Physical drive nmuber to identify the drive */
)
{
	DSTATUS stat;
	int result;

	switch (pdrv) {
	case FF_SD_CARD :
        if (dev_sdcard_get_status() == DEV_SD_TRANSFER_OK)
        {
            result = RES_OK;
        }
        else
        {
            result = RES_ERROR;
        }

		// translate the reslut code here
        stat = (DSTATUS) result;

		return stat;

	case FF_SPI_FLASH :
		result = RES_NOTRDY;

		// translate the reslut code here
        stat = (DSTATUS) result;

		return stat;

	case FF_NAND_FLASH :
		result = RES_NOTRDY;

		// translate the reslut code here
        stat = (DSTATUS) result;

		return stat;
	}
	return STA_NOINIT;
}



/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
	BYTE pdrv				/* Physical drive nmuber to identify the drive */
)
{
	DSTATUS stat;
	int result;

	switch (pdrv) {
	case FF_SD_CARD :
		result = dev_sdcard_init();

		// translate the reslut code here
        stat = (DSTATUS) result;

		return stat;

	case FF_SPI_FLASH :
		result = dev_spiflash_init();

		// translate the reslut code here
        stat = (DSTATUS) result;

		return stat;

	case FF_NAND_FLASH :
		result = RES_NOTRDY;

		// translate the reslut code here
        stat = (DSTATUS) result;

		return stat;
	}
	return STA_NOINIT;
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/

DRESULT disk_read (
	BYTE pdrv,		/* Physical drive nmuber to identify the drive */
	BYTE *buff,		/* Data buffer to store read data */
	LBA_t sector,	/* Start sector in LBA */
	UINT count		/* Number of sectors to read */
)
{
	DRESULT res;
	int result;

    /* 数据校验 */
    if ((buff == NULL) || (count == 0))
    {
        return RES_PARERR;
    }

	switch (pdrv) {
	case FF_SD_CARD :
		// translate the arguments here

		result = dev_sdcard_read_disk_dma(buff, sector, count);

		// translate the reslut code here
        res = (DSTATUS) result;

		return res;

	case FF_SPI_FLASH :
		// translate the arguments here

		result = RES_NOTRDY; //RAM_disk_status();

		// translate the reslut code here
        res = (DSTATUS) result;

		return res;

	case FF_NAND_FLASH :
		// translate the arguments here

		result = RES_NOTRDY; //RAM_disk_status();

		// translate the reslut code here
        res = (DSTATUS) result;

		return res;
	}

	return RES_PARERR;
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */
/*-----------------------------------------------------------------------*/

#if FF_FS_READONLY == 0

DRESULT disk_write (
	BYTE pdrv,			/* Physical drive nmuber to identify the drive */
	const BYTE *buff,	/* Data to be written */
	LBA_t sector,		/* Start sector in LBA */
	UINT count			/* Number of sectors to write */
)
{
	DRESULT res;
	int result;

    /* 数据校验 */
    if ((buff == NULL) || (count == 0))
    {
        return RES_PARERR;
    }

	switch (pdrv) {
	case FF_SD_CARD :
		// translate the arguments here

		result = dev_sdcard_write_disk_dma(buff, sector, count);

		// translate the reslut code here
        res = (DSTATUS) result;

		return res;

	case FF_SPI_FLASH :
		// translate the arguments here

		result = RES_NOTRDY; //RAM_disk_status();

		// translate the reslut code here
        res = (DSTATUS) result;

		return res;

	case FF_NAND_FLASH :
		// translate the arguments here

		result = RES_NOTRDY; //RAM_disk_status();

		// translate the reslut code here
        res = (DSTATUS) result;

		return res;
	}

	return RES_PARERR;
}

#endif


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

DRESULT disk_ioctl (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE cmd,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
	DRESULT res;
	int result;

	switch (pdrv) {
	case FF_SD_CARD :

		// Process of the command for the RAM drive

		return res;

	case FF_SPI_FLASH :

		result = RES_OK; //RAM_disk_status();

		// translate the reslut code here
        res = (DSTATUS) result;

		return res;

	case FF_NAND_FLASH :

		result = RES_OK; //RAM_disk_status();

		// translate the reslut code here
        res = (DSTATUS) result;

		return res;
	}

	return RES_PARERR;
}

//User defined function to give a current time to fatfs module      */
//31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Date (1-31) */                                                                                                                                                                                                                                          
//15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */         
DWORD get_fattime (void)
{
    soc_rtc_t * rtc = NULL;
    DWORD       time = 0;

    /* 获得当前时间 */
    rtc = soc_rtc_gettime();

    if (rtc == NULL)
    {
        return 0;
    }

    /* 组装时间 */
    time |= ((DWORD) rtc->year << 25);
    time |= ((DWORD) rtc->month << 21);
    time |= ((DWORD) rtc->date << 16);
    time |= ((DWORD) rtc->hour << 11);
    time |= ((DWORD) rtc->minute << 5);
    time |= ((DWORD) rtc->second << 0);

    return time;
}
